﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QuanLyDiem.models;
namespace QuanLyDiem.QLDaoTao
{
    public partial class frmBaoLuu : Form
    {
        public frmBaoLuu()
        {
            InitializeComponent();
        }

        private void frmBaoLuu_Load(object sender, EventArgs e)
        {
            try
            {
                frmChuyenKhoa a = new frmChuyenKhoa();
                Context db = new Context();
                var dsmon1 = db.LopHocPhans.Where(p => p.ID_lop == frmChuyenKhoa.id_lop1s).ToList();
                var id_chuyennganh2 = db.Lops.Where(p => p.ID_lop == frmChuyenKhoa.id_lop2s).Select(o => o.ID_chuyen_nganh).First();
              
                var id_dt2 = db.ChuongTrinhDaoTaos.Where(p => p.ID_chuyen_nganh == id_chuyennganh2).Select(o => o.ID_dt).First();
                
                var dsmon2 = new List<MonHoc>();
                foreach (var row in dsmon1)
                {
                    var id_mon = db.ChuongTrinhDaoTaoChiTiets.Where(p => p.ID_dt == id_dt2 && p.ID_mon == row.ID_mon).Select(o => o.ID_mon).FirstOrDefault();
                    if (id_mon != 0)
                    {
                        var monhoc = db.MonHocs.Where(p => p.ID_mon == id_mon).First();
                        dsmon2.Add(monhoc);
                    }
                }
                monHocBindingSource.DataSource = dsmon2;
            }
            catch 
            { 
            }
            
        }

        private void btnBaoLuu_Click(object sender, EventArgs e)
        {
            Context db = new Context();

            for (int i = 0; i < gridView1.RowCount; i++)
            {
                var row1 = gridView1.GetRow(i) as MonHoc;
                var id_lophp1 = db.LopHocPhans.Where(p => p.ID_lop == frmChuyenKhoa.id_lop1s && p.ID_mon == row1.ID_mon).Select(o => o.ID_lop_hp).First();
                var id_lophp2 = db.LopHocPhans.Where(p => p.ID_lop == frmChuyenKhoa.id_lop2s && p.ID_mon == row1.ID_mon).Select(o => o.ID_lop_hp).First();
                foreach (var row in frmChuyenKhoa.danhsachlop)
                {
                    
                    var dslophp = db.DanhSachLopHocPhans.Where(p => p.ID_sv == row.ID_sv && p.ID_lop_hp == id_lophp1).First();
                    dslophp.ID_lop_hp = id_lophp2;
                    db.SubmitChanges();
                }
            }
            
            
        }
    }
}
